//
//  EnemyManager.swift
//  ShapeSurvival
//
//  Created by Jerry H on 1/15/15.
//  Copyright (c) 2015 HeylmunProgramming. All rights reserved.
//

import Foundation; import SpriteKit;

class EnemyManager {
    var scene: AnyObject = 0;
    var currentEnemies: [Enemy] = [];
    var currentExplosions: [Explosion] = [];
    var futureExplosions: [Explosion] = [];
    var enemiesSpawned = 0
    var spawnWave: CGFloat = 0.0
    
    
    func spawnEnemies(){
        /*
        if(enemiesSpawned == 0){
        enemiesSpawned = enemiesSpawned + 1;
        createEnemy(3)
        }
    */
        
        if(runTime > 1.0 * CGFloat(spawnWave)){
            spawnWave = spawnWave + 1
            if(spawnWave > 15){
                if(spawnWave > 20){
                    if(spawnWave % 5 == 0.0 ) {
                        enemiesSpawned = enemiesSpawned + 1;
                        createEnemy(10)
                    }
                }
            }
            else{
                if(spawnWave % 2 == 0.00) {
                    enemiesSpawned = enemiesSpawned + 1;
                    createEnemy(1)
                }
                if(spawnWave % 8 == 0.0 ) {
                    enemiesSpawned = enemiesSpawned + 1;
                    createEnemy(2)
                }
                if(spawnWave % 15 == 0.0 ) {
                    enemiesSpawned = enemiesSpawned + 1;
                    createEnemy(4)
                }
                if(spawnWave % 20 == 0.0 ) {
                    enemiesSpawned = enemiesSpawned + 1;
                    createEnemy(10)
                }
            }
        }
        
        
    }
    

    
    func createEnemy(Etype: Int) {
        var returning: Enemy //= Enemy(imageNamed: "Simple");
        let rand = Int(arc4random_uniform(4))
        var setX: Int = 250;
        var setY: Int = 250;
        var ang: CGFloat = 0;
        if(rand == 0){
            //Put in random position above top
            setX = Int(arc4random_uniform(UInt32(CGRectGetMaxX(playScene.frame) * 1.2 )))
            setY = Int(CGRectGetMaxY(playScene.frame) * 1.2)
            ang = 180
        }
        else if(rand == 1){
            //Put in random position below bot
            setX = Int(arc4random_uniform(UInt32(CGRectGetMaxX(playScene.frame) * 1.2 )))
            setY = Int( 0 - ( (CGRectGetMaxY(playScene.frame) * 1.2) - (CGRectGetMaxY(playScene.frame) )))
            ang = 0
            
        }
        else if(rand == 2){
            //Put in random position out of left
            setY = Int(arc4random_uniform(UInt32(CGRectGetMaxY(playScene.frame) * 1.2 )))
            setX = Int( 0 - ( (CGRectGetMaxX(playScene.frame) * 1.2) - (CGRectGetMaxX(playScene.frame) )))
            ang = 270
        }
        else if(rand == 3){
            //Put in random position out of right
            setY = Int(arc4random_uniform(UInt32(CGRectGetMaxY(playScene.frame) * 1.2 )))
            setX = Int(CGRectGetMaxX(playScene.frame) * 1.2)
            ang = 90
        }
        if(Etype == 1){
            //Basic Enemy
            returning = Diamond(imageNamed: "Simple")
            returning.setEnemy(CGFloat(setX), Ypos: CGFloat(setY), startingAngle: ang)
        }
        else if (Etype == 2){
            //Speeder
            returning = Speeder(imageNamed: "Speed")
            returning.setEnemy(CGFloat(setX), Ypos: CGFloat(setY), startingAngle: ang)
            
        }
        else if (Etype == 3){
            //Gunner
            returning = Gunner(imageNamed: "GunnerBase")
            returning.setEnemy(CGFloat(setX), Ypos: CGFloat(setY), startingAngle: ang)
        }
        else if (Etype == 4){
            //Tank
            returning = Tank(imageNamed: "TankFacing")
            returning.setEnemy(CGFloat(setX), Ypos: CGFloat(setY), startingAngle: ang)
            
        }
        else
        if (Etype == 10){
            //Rocket
            returning = Rocket(imageNamed: "Rocket")
            returning.setEnemy(CGFloat(setX), Ypos: CGFloat(setY), startingAngle: ang)
            
        }

        //Other types here
    }
    
    func moveEnemies(){
        for enemy: Enemy in currentEnemies {
            enemy.moveSelf(player.position.x, PlayerY: player.position.y)
            if(enemy.type == 10){
                if(enemy.distanceToPlayer() < 20){
                    enemy.die()
                }
            }
            
        }
    }
}